package org.example.what_mall.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.what_mall.dto.*;
import org.example.what_mall.entity.Category;
import org.example.what_mall.exception.GlobalCode;
import org.example.what_mall.result.HttpResult;
import org.example.what_mall.service.ICommentService;
import org.example.what_mall.service.IMallService;
import org.example.what_mall.vo.GoodsVO;
import org.example.what_mall.vo.MsgReplyVO;
import org.example.what_mall.vo.OrderVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "商城接口")
@RestController
@RequestMapping("/api/mall")
@RequiredArgsConstructor
public class MallController {
    private final IMallService mallService;
    private final ICommentService commentService;

    @Operation(summary = "获取所有商品类型")
    @GetMapping("/getType")
    public HttpResult<List<Category>> getType() {
        return HttpResult.success(mallService.getTypes());
    }

    @Operation(summary = "根据商品类型获取商品")
    @GetMapping("/getGoodsByType")
    public HttpResult<List<GoodsVO>> getGoodsByType(@RequestParam("typeId") Integer typeId) {
        return HttpResult.success(mallService.getGoodsByType(typeId));
    }

    @Operation(summary = "获取商品信息")
    @GetMapping("getGoodsInfo")
    public HttpResult<GoodsDTO> getGoodsInfo(@RequestParam("id") Integer id ) {
        return HttpResult.success(mallService.getGoodsInfo(id));
    }

    @Operation(summary = "搜索商品")
    @GetMapping("searchGoods")
    public HttpResult<List<GoodsVO>> searchGoods(@RequestParam("keyword") String keyword) {
        return HttpResult.success(mallService.searchGoods(keyword));
    }

    @Operation(summary = "添加订单")
    @PostMapping("/addOrder")
    public HttpResult<Void> addOrder(@RequestBody OrderForm form) {
        mallService.addOrder(form);
        return HttpResult.ok();
    }

    @Operation(summary = "根据订单状态获取订单")
    @GetMapping("/getOrderByState")
    public HttpResult<List<OrderVO>> getOrderByState(@RequestParam("state") Integer state, @RequestParam("token") String token) {
        return HttpResult.success(mallService.getOrderByState(state, token));
    }

    @Operation(summary = "完成订单")
    @PostMapping("/settleAccounts")
    public HttpResult<Void> settleAccounts(@RequestBody SettleForm form) {
        mallService.settleAccounts(form);
        return HttpResult.ok();
    }

    @Operation(summary = "确认收取")
    @GetMapping("/confirmReceive")
    public HttpResult<Void> confirmReceive(@RequestParam("id") Integer id) {
        mallService.confirmReceive(id);
        return HttpResult.ok();
    }


    @Operation(summary = "询问商品信息")
    @PostMapping("/askGoodsMsg")
    public HttpResult<Void> askGoodsMsg(@RequestBody MessageForm form) {
        commentService.askGoodsMsg(form);
        return HttpResult.ok();
    }

    @Operation(summary = "获取商品询问信息")
    @GetMapping("/getGoodsMsg")
    public HttpResult<List<MsgReplyVO>> getGoodsMsg(@RequestParam("id") Integer id) {
        return HttpResult.success(commentService.getGoodsMsg(id));
    }


    @Operation(summary = "发送商品评论")
    @PostMapping("/sendComment")
    public HttpResult<Void> sendComment(@RequestBody CommentForm form) {
        commentService.sendComment(form);
        return HttpResult.ok();
    }

    @Operation(summary = "获取商品评论")
    @GetMapping("/getGoodsComment")
    public HttpResult<CommentListDTO> getGoodsComment(@RequestParam("goodsId") Integer goodsId) {
        return HttpResult.success(commentService.getGoodsComment(goodsId));
    }


}
